package com.hspedu.class_;

import com.hspedu.Car;

/**
 * @Author Stringzhua
 * @Date 2024/5/5 19:03
 * description:
 */
public class GetClass {
    public static void main(String[] args) throws ClassNotFoundException {
        //1.Class.forName
        String classAllPath = "com.hspedu.Car";//通过读取配置文件读取
        Class<?> cls1 = Class.forName(classAllPath);
        System.out.println(cls1);

        //2.类名.class 应用场景:多用于参数传递
        Class cls2 = Car.class;
        System.out.println(Car.class);

        //3.对象.getClass(),应用场景:有对象实例
        Car car = new Car();
        Class cls3 = car.getClass();
        System.out.println(cls3);

        //4.通过类加载器【四种】,来获取到类的Class对象
        //step1:先得到类加载器  Car
        ClassLoader classLoader = car.getClass().getClassLoader();
        //step2:通过类加载器得到Class对象
        Class cls4 = classLoader.loadClass(classAllPath);
        System.out.println(cls4);

        //cls1,cls2,cls3,cls4 其实是同一个对象
        System.out.println(cls1.hashCode());
        System.out.println(cls2.hashCode());
        System.out.println(cls3.hashCode());
        System.out.println(cls4.hashCode());

        //5.基本数据类型(byte,short,int,long,float,double,boolean,char)按如下方式得到Class类对象
        Class<Integer> integerClass = int.class;
        Class<Boolean> booleanClass = boolean.class;
        Class<Character> characterClass = char.class;

        System.out.println(integerClass);//int

        //6.基本数据类型对应的包装类,可以通过.TYPE得到class对象
        Class<Integer> type1 = Integer.TYPE;
        Class<Character> type2 = Character.TYPE;//其他包装类BOOLEAN,DOUBLE,LONG,BYTE等包装类

        System.out.println(type1);//int
        System.out.println(integerClass.hashCode());//1956725890
        System.out.println(type1.hashCode());//1956725890
    }
}
